package com.hnevc.wechatviewdemo;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    public static final String TAG="MainActivity";

    //声明按键的变量
    LinearLayout mTabWeixin,mTabLianxian,mTabFaxian,mTabWode;
    Fragment mWeixinFragment,mLianxirenFragment,
        mFaxianFragment,mWodeFragment;
    ViewPager viewPager;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //从布局文件中，获取控件
        mTabWeixin=(LinearLayout) findViewById(R.id.id_weixin_tab);
        mTabLianxian=(LinearLayout) findViewById(R.id.id_lianxiren_tab);
        mTabFaxian=(LinearLayout) findViewById(R.id.id_faxian_tab);
        mTabWode=(LinearLayout) findViewById(R.id.id_wode_tab);
      viewPager =(ViewPager)findViewById(R.id.viewpager);
        //将TabWeixin控件设置为选中状态
        mTabWeixin.setSelected(true);

        //新建Fragment对象
        mWeixinFragment=new WeixinFragment();
        mLianxirenFragment=new LianxirenFragment();
        mFaxianFragment=new FaxianFragment();
        mWodeFragment=new WodeFragment();
        //Fragment添加到List中
        final List<Fragment> fragmentList=new ArrayList<>();
        fragmentList.add(mWeixinFragment);
        fragmentList.add(mLianxirenFragment);
        fragmentList.add(mFaxianFragment);
        fragmentList.add(mWodeFragment);

     //创建适配器
        FragmentPagerAdapter adapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                //返回position位置需要显示的Fragment
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };
        viewPager.setAdapter(adapter);
       // viewPager.setCurrentItem(3);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){

            public void onPageScrolled(int position,float positionOffice,int positionOffsetPixels){}

                public void onPageSelected(int position){
                Log.e(TAG,"onPageSelected:"+position);
                if(position==0){
                    mTabWeixin.setSelected(true);
                    mTabLianxian.setSelected(false);
                    mTabFaxian.setSelected(false);
                    mTabWode.setSelected(false);
                }
                if(position==1){
                    mTabLianxian.setSelected(true);
                    mTabWeixin.setSelected(false);
                    mTabFaxian.setSelected(false);
                    mTabWode.setSelected(false);

                }
                if(position==2){
                    mTabFaxian.setSelected(true);
                    mTabLianxian.setSelected(false);
                    mTabWeixin.setSelected(false);
                    mTabWode.setSelected(false);

                }
                if(position==3){
                    mTabWode.setSelected(true);
                    mTabLianxian.setSelected(false);
                    mTabFaxian.setSelected(false);
                    mTabWeixin.setSelected(false);

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void tabClick(View view) {
         int id=view.getId();
        switch (id){
            case R.id.id_weixin_tab:
                viewPager.setCurrentItem(0);
                mTabWeixin.setSelected(true);
                mTabLianxian.setSelected(false);
                mTabFaxian.setSelected(false);
                mTabWode.setSelected(false);
                break;
            case R.id.id_lianxiren_tab:
                viewPager.setCurrentItem(1);
                mTabLianxian.setSelected(true);
                mTabWeixin.setSelected(false);
                mTabFaxian.setSelected(false);
                mTabWode.setSelected(false);
                break;
            case R.id.id_faxian_tab:
                viewPager.setCurrentItem(2);
                mTabFaxian.setSelected(true);
                mTabLianxian.setSelected(false);
                mTabWeixin.setSelected(false);
                mTabWode.setSelected(false);
                break;
            case R.id.id_wode_tab:
                viewPager.setCurrentItem(3);
                mTabWode.setSelected(true);
                mTabLianxian.setSelected(false);
                mTabFaxian.setSelected(false);
                mTabWeixin.setSelected(false);
                break;
        }

    }
}
